import storage from '@/assets/utils/Storage'
import routers from './routers'
const Storage = storage.storage

const router = {
	navigateBack(number){ //返回
		mpvue.navigateBack({
			delta: number || 1
		})
	},
	navigateTo(url, data){ //保留当前页面，跳转
		let option = this.getOption(data)
		if (this.isLogin(url)) {
			mpvue.navigateTo({
				url: '/pages/'+url+'/main'+option
			})
		}
		
	},
	redirectTo(url, data){ //关闭当前页面，跳转
		let option = this.getOption(data)
		if (this.isLogin(url)) {
			mpvue.redirectTo({
				url: '/pages/'+url+'/main'+option
			})
		}
	},
	reLaunch(url, data){ //关闭所有页面，跳转
		let option = this.getOption(data)
		if (this.isLogin(url)) {
			mpvue.reLaunch({
				url: '/pages/'+url+'/main'+option
			})
		}
	},
	switchTab(url, data){ //跳转导航
		if (this.isLogin(url)) {
			mpvue.switchTab({
				url: '/pages/'+url+'/main'
			})
		}
	},
	isLogin(url){ //判断是否需要登录
		let val = true
		for (var i = 0; i < routers.length; i++) {
			if (routers[i].pach == url) {
				if (routers[i].isLogin) {
					if (Storage.getStorage('toKen')) {
						val = true
					}else{
						this.redirectTo('login')
						val = false
					}
				}
				break;
			}
		}
		return val
	},
	getOption(data){ //解析参数
		let option = '?'
		if (data) {
			for(let name in data){
				option += name + '=' + data[name] +'&'
			}
		}
		return option
	}
}

export default {
	install: function(Vue, Option) {
		Object.defineProperty(Vue.prototype, "$router", { value: router });
	},
	router:router
};